package com.example.controller.product;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@Controller
@RequestMapping("/upload")
public class UploadFile {

    @Value("${file.path}")
    private String path;

    /**
     * @Description: fileupload上传文件
     * @Param: file文件
     * @return: java.util.Map<java.lang.String,java.lang.String>
     * @Author: yc
     * @Date: 2022/11/12 16:45
     */
    @CrossOrigin
    @ResponseBody
    @RequestMapping("/uploadfile")
    public Map<String,String> fileupload(MultipartFile file){
        String originalFilename = file.getOriginalFilename();
        System.out.println(originalFilename);

        int i = originalFilename.indexOf(".");
        String substring = originalFilename.substring(i);

        UUID uuid = UUID.randomUUID();

        File file1 = new File(path + uuid + "." + substring);

        try {
            //上传保存到file1中
            file.transferTo(file1);
        } catch (IOException e) {
            e.printStackTrace();
        }
        Map<String, String> map = new HashMap<>();
        map.put("msg","成功");
        return map;
    }
}
